import { GameDataManager } from "../../Framework/Managers/GameDataManager";
import { ClientManager } from "../../Framework/Managers/ClientManager";
import { ClientName } from "../../Framework/Enums/ClientName";
import { GameCommond } from "../Command/GameCommond";

export class WriteCommand {
    public static ClassName:string = "WriteCommand";
    
    /** 此方法,直接new 传入方法名即可, 参数按照顺序方式传入 */
    constructor(name:string,...args:any[]) {
        if(GameDataManager.game.debugMode)return
        if(typeof name != "string")return
        let func : Function = null
        if(name.search("write")==-1){func = this.constructor["write" + name]}else{func = this.constructor[name]}
        if(func == undefined){console.error("调用一个未知方法,请检查后重试~!");return}
        let sendData = func.apply(this,args)
        if(sendData == "" || sendData.length <= 0){console.log("消息长度不对");return}
        ClientManager.SendMessage(ClientName.GAME_CLIENT, sendData,0);
    }

    private static writeLoginServer () : any {
        let writeData = {id:"login",data:{"safecode":GameDataManager.game.playerId}}
        console.log("%c 连接到服务器成功,发送登录数据 -> ","color:red",writeData)
        return writeData
    }

    //创建桌子
    private static writeCreaterDesk(custom:any) : any {
        let writeData = {id:"qpmj",action:GameCommond.Game_CreaterDesk,data:{clubid:"0",opts:{round:10}}}
        return writeData
    } 

    private static writeJoinDesk (deskno : string) : any {
        let writeData : any = {id:GameCommond.Game_JoinDesk,deskId:String(deskno)}
        console.log("%c 申请加入桌子,桌号:%s","blue",deskno)
        return writeData
    }

    //发送重新加入房间
    private static writeReJoinDesk() : any {
        if(GameDataManager.game.deskno == "" || GameDataManager.game.curGame == "")return
        let writeData : any = {id:GameDataManager.game.curGame,action:GameCommond.Game_ReJoin,
            data:{deskId:GameDataManager.game.deskno}}
            console.log("%c 申请重新加入桌子,桌号:%s","blue",GameDataManager.game.deskno)
        return writeData

    }

    //发送准备
    private static writeReady() : any {
        if(GameDataManager.game.deskno == "" || GameDataManager.game.curGame == "")return
        let writeData : any = {id:GameDataManager.game.curGame,action:GameCommond.Game_Ready}
        return writeData
    }

    //发送齐牌完毕
    // new WriteCommand(WriteCmdEvent.CMD_Ready)
    
    private static writeQiPaiFinished() : any {
        if(GameDataManager.game.deskno == "" && GameDataManager.game.curGame == "")return
        let writeData : any = {id:GameDataManager.game.curGame,action:GameCommond.Game_QiPai}
        return writeData

    }

    //发送操作
    private static writeOpChoose(optype:number ,index:number) : any {
        if(GameDataManager.game.deskno == "")return
        let game = GameDataManager.game.curGame
        let writeData : any = {id:game,action:GameCommond.Game_OpChoose,data:{optype:optype,idx:index}}
        return writeData
    }

}